package com.hqs.listener.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.hqs.listener.R;
import com.hqs.listener.model.ItemModel;
import com.hqs.listener.view.FileListItemView;

import java.util.ArrayList;

/**
 * Created by super on 2017/11/14.
 */

public class FileListAdapter extends BaseAdapter {

    ArrayList<ItemModel> files;
    Context context;
    int colorNormal;
    int colorSelected;

    public FileListAdapter(Context context, ArrayList<ItemModel> files) {
        this.files = files;
        this.context = context;
        colorNormal = context.getResources().getColor(R.color.textColorNormal);
        colorSelected = context.getResources().getColor(R.color.textColorSelected);
    }

    @Override
    public int getCount() {
        if (files == null){
            return 0;
        }
        return files.size();
    }

    @Override
    public Object getItem(int i) {
        return files.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        FileListItemView fileListItemView;

        if (view != null){
            fileListItemView = (FileListItemView) view;
        }
        else {
            fileListItemView = (FileListItemView) LayoutInflater.from(context).inflate(R.layout.item_file_list, null);
            fileListItemView.init();
        }

        fileListItemView.setTitle(files.get(i).fileName);
        if (files.get(i).isPlaying){
            fileListItemView.tvTitle.setTextColor(colorSelected);
        }
        else {
            fileListItemView.tvTitle.setTextColor(colorNormal);
        }
        return fileListItemView;
    }
}
